<title>我要提现</title>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">

        <div class="layui-card-body">
          <div class="LAY_wisdom_title">
            <h3 class="LAY_wisdom_title_line">结算周期</h3>
          </div>

          <table id="LAY_withdraw_period" lay-filter="LAY_withdraw_period"></table>
          
          <div class="LAY_withdraw_btn_group">
              <button class="layui-btn LAY_withdraw_btn_search">查询账单</button>
          </div>

          <div class="LAY_wisdom_title">
            <h3>搜索结果</h3>
          </div>

          <table id="LAY_withdraw_bill" lay-filter="LAY_withdraw_bill"></table>
          <div class="LAY_withdraw_amount">
            <p>合计：<span>0</span>元</p>
            <p>请开票金额与合并金结算额保持严格一致</p>
          </div>
          
          <div class="LAY_withdraw_btn_group">
              <button class="layui-btn LAY_withdraw_btn_apply">申请提现</button>
              <button class="layui-btn layui-btn-primary LAY_withdraw_btn_back">返回</button>
          </div>
        </div>

      </div>
    </div>
  </div>
</div>

<script>
  layui.use(['admin', 'laytpl', 'table', 'layer'], function () {
    var $ = layui.$,
      admin = layui.admin,
      setter = layui.setter,
      layer = layui.layer,
      table = layui.table,
      laytpl = layui.laytpl,
      element = layui.element,
      params = {
        settlementCycle: '',
        amount: 0
      };

    table.render({
      elem: '#LAY_withdraw_period'
      ,url: setter.url + '/finance/settlementRecord' //模拟接口
      ,page: false
      ,limit: Number.MAX_VALUE
      ,parseData: function(res){
        var array = params.settlementCycle.split(',');
        if (array) {
          for(var i in res.data){
            if(array.indexOf(res.data[i].settlementCycle) > -1){
              res.data[i]["LAY_CHECKED"] = true;
            }
          }
        }

        return {
          "code": res.code,
          "msg": res.msg,
          "data": res.data
        };
      }
      ,response: {
        statusCode: 200 //规定成功的状态码，默认：0
      }
      ,cols: [[
        {type: 'checkbox', align: 'center',}
        ,{ field: 'settlementCycle', align: 'center', title: '结算周期' }
        ,{ field: 'settlementAmount', align: 'center', title: '结算金额(元)' }
        ,{ field: 'status', align: 'center', title: '结算状态' }
      ]]
    });

    table.on('checkbox(LAY_withdraw_period)', function(obj){
      var amount = getAmount();
      var settlementCycle = getSettlementCycle();
      params = {
        settlementCycle: settlementCycle,
        amount: amount
      };
      $('.LAY_withdraw_amount span').html(amount);
    });
    
    initBillList();
    initEvents();

    function initBillList() {
      table.render({
        elem: '#LAY_withdraw_bill'
        ,page: true
        ,data: []
        ,cols: [[
          { field: 'generateTime', align: 'center', title: '生成日期' }
          ,{ field: 'channel', align: 'center', title: '下单渠道' }
          ,{ field: 'policyCode', align: 'center', title: '保单号' }
          ,{ field: 'commType', align: 'center', title: '推广费类型' }
          ,{ field: 'createTime', align: 'center', title: '下单日期' }
          ,{ field: 'renewalTime', align: 'center', title: '续期应交日期' }
          ,{ field: 'realRenewalTime', align: 'center', title: '续期实交日期' }
          ,{ field: 'status', align: 'center', title: '推广责任状态' }
          ,{ field: 'inAccountTime', align: 'center', title: '入账时间' }
          ,{ field: 'insPremium', align: 'center', title: '保费(元)' }
          ,{ field: 'commissionRate', align: 'center', title: '结算比例' }
          ,{ field: 'commission', align: 'center', title: '推广费(元)' }
          ,{ field: 'holder', align: 'center', title: '投保人姓名' }
          ,{ field: 'insured', align: 'center', title: '被保人姓名' }
        ]]
      });
    }

    function reloadBillList(settlementCycle, curr, callback) {
      table.reload('LAY_withdraw_bill', {
        page: {
          curr: curr
        }
        ,url: setter.url + '/finance/settlementBill' //模拟接口
        ,where: { 
          settlementCycle: settlementCycle
        }
        ,parseData: function(res){
          return {
            "code": res.code,
            "msg": res.msg,
            "data": res.data.datas,
            "count": res.data.recordCounts,
            "amount": res.data.totalAmount
          };
        }
        ,response: {
          statusCode: 200 //规定成功的状态码，默认：0
        } 
        ,request: {
          limitName: 'pageSize'
        }
        ,done: function (res) {
          if (typeof(callback) == 'function') {
            callback(res);
          }
        }
      })
    }

    function getAmount() {
      var checkStatus = layui.table.checkStatus('LAY_withdraw_period').data;
      var result = 0;
      layui.each(checkStatus, function (index, item) {
        result += Number(item.settlementAmount);
      });

      return result;
    }

    function getSettlementCycle() {
      var checkStatus = layui.table.checkStatus('LAY_withdraw_period').data;
      var result = [];

      for(var i=0; i < checkStatus.length; i++){
        result.push(checkStatus[i].settlementCycle);
      }

      result = result.toString();

      return result;
    }

    function initEvents() {
      $('.LAY_withdraw_btn_back').off('click').on('click', function () {
        admin.events.back();
      });

      $('.LAY_withdraw_btn_search').off('click').on('click', function () {
        var settlementCycle = getSettlementCycle();

        if (!settlementCycle) {
          layer.msg('未选择结算周期', {
            icon: 2,
            time: 1500,
            shade: [0.3, '#393D49']
          });
          return;
        }

        reloadBillList(settlementCycle, 1);
      });

      $('.LAY_withdraw_btn_apply').off('click').on('click', function () {
        if (params.amount <= 0) {
          layer.msg('您没有可以提现的金额', {
            icon: 2,
            time: 1500,
            shade: [0.3, '#393D49']
          });
          return;
        }

        admin.req({
          url: setter.url + '/finance/applyWithdrawal',
          type: 'POST',
          data: params,
          done: function (res) {
            var data = res.data || [];
            var icon = 2;
            var msg = '提现申请失败';

            if (data) {
              icon = 1;
              msg = '提现申请成功';
            }

            layer.msg(msg, {
              icon: icon,
              time: 1500,
              shade: [0.3, '#393D49']
            });
            
            table.reload('LAY_withdraw_period');
          }
        });
      });
    }

  });
</script>